{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Self-Consistency and Multiple Paths of Reasoning Tutorial\n",
    "\n",
    "## Overview\n",
    "\n",
    "This tutorial explores the concept of self-consistency and multiple paths of reasoning in prompt engineering. We'll focus on techniques for generating diverse reasoning paths and aggregating results to improve the quality and reliability of AI-generated answers.\n",
    "\n",
    "## Motivation\n",
    "\n",
    "Large language models can sometimes produce inconsistent or unreliable outputs. By leveraging multiple reasoning paths and aggregating results, we can enhance the robustness and accuracy of AI-generated responses. This approach is particularly useful for complex problem-solving tasks where a single path of reasoning might be insufficient or prone to errors.\n",
    "\n",
    "## Key Components\n",
    "\n",
    "1. Generating multiple reasoning paths\n",
    "2. Aggregating results for better answers\n",
    "3. Implementing self-consistency checks\n",
    "4. Applying these techniques to various problem-solving scenarios\n",
    "\n",
    "## Method Details\n",
    "\n",
    "Our approach involves the following steps:\n",
    "\n",
    "1. Setting up the environment with necessary libraries (OpenAI and LangChain)\n",
    "2. Designing prompts that encourage diverse reasoning paths\n",
    "3. Generating multiple responses using these prompts\n",
    "4. Implementing aggregation methods to combine and analyze the generated responses\n",
    "5. Applying self-consistency checks to evaluate the reliability of the results\n",
    "6. Demonstrating the effectiveness of this approach on various problem types\n",
    "\n",
    "Throughout the tutorial, we'll use practical examples to illustrate how these techniques can be applied to enhance the quality and reliability of AI-generated answers.\n",
    "\n",
    "By the end of this tutorial, you'll have a solid understanding of how to implement self-consistency and multiple paths of reasoning in your prompt engineering workflows, leading to more robust and reliable AI-generated responses.\n",
    "\n",
    "## Conclusion\n",
    "\n",
    "This tutorial will equipped you with powerful techniques for enhancing the reliability and consistency of AI-generated responses through self-consistency and multiple paths of reasoning. By implementing these methods, you can:\n",
    "\n",
    "1. Generate diverse problem-solving approaches, reducing the risk of biased or narrow solutions.\n",
    "2. Aggregate multiple reasoning paths to arrive at more robust and reliable answers.\n",
    "3. Apply self-consistency checks to evaluate and improve the quality of AI-generated outputs.\n",
    "4. Adapt these techniques to various problem types, from factual queries to complex reasoning tasks.\n",
    "\n",
    "Mastering these skills will significantly improve your ability to leverage AI language models for more accurate and trustworthy results across a wide range of applications. As you continue to explore and refine these techniques, you'll be better equipped to handle complex problems and generate high-quality, consistent outputs in your AI-driven projects."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "First, let's import the necessary libraries and set up our environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.prompts import PromptTemplate\n",
    "from dotenv import load_dotenv\n",
    "import random\n",
    "from collections import Counter\n",
    "\n",
    "# Load environment variables\n",
    "load_dotenv()\n",
    "\n",
    "# Set up OpenAI API key\n",
    "os.environ[\"OPENAI_API_KEY\"] = os.getenv('OPENAI_API_KEY')\n",
    "\n",
    "# Initialize the language model\n",
    "llm = ChatOpenAI(model=\"gpt-4o-mini\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Generating Multiple Reasoning Paths\n",
    "\n",
    "Let's create a function that generates multiple reasoning paths for a given problem."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_multiple_paths(problem, num_paths=3):\n",
    "    \"\"\"\n",
    "    Generate multiple reasoning paths for a given problem.\n",
    "    \n",
    "    Args:\n",
    "    problem (str): The problem statement.\n",
    "    num_paths (int): Number of reasoning paths to generate.\n",
    "    \n",
    "    Returns:\n",
    "    list: A list of generated reasoning paths.\n",
    "    \"\"\"\n",
    "    prompt_template = PromptTemplate(\n",
    "        input_variables=[\"problem\", \"path_number\"],\n",
    "        template=\"\"\"Solve the following problem using a unique approach. This is reasoning path {path_number}.\n",
    "        Problem: {problem}\n",
    "        Reasoning path {path_number}:\"\"\"\n",
    "    )\n",
    "\n",
    "    paths = []\n",
    "    for i in range(num_paths):\n",
    "        chain = prompt_template | llm\n",
    "        response = chain.invoke({\"problem\": problem, \"path_number\": i+1}).content\n",
    "        paths.append(response)\n",
    "    \n",
    "    return paths"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, let's test our function with a sample problem."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Path 1:\n",
      "To solve the problem of how high a ball will go when thrown upwards with an initial velocity of 20 m/s, we can use the principles of kinematics, particularly the equations of motion under constant acceleration due to gravity.\n",
      "\n",
      "### Reasoning Path 1:\n",
      "\n",
      "1. **Identify the Variables:**\n",
      "   - Initial velocity (\\(v_0\\)) = 20 m/s (upward)\n",
      "   - Final velocity (\\(v\\)) at the highest point = 0 m/s (the ball stops rising at the peak)\n",
      "   - Acceleration due to gravity (\\(g\\)) = -9.81 m/s² (negative because it acts downward)\n",
      "\n",
      "2. **Use the Kinematic Equation:**\n",
      "   We can use the following kinematic equation that relates initial velocity, final velocity, acceleration, and displacement (height in this case):\n",
      "\n",
      "   \\[\n",
      "   v^2 = v_0^2 + 2a s\n",
      "   \\]\n",
      "\n",
      "   Here, \\(s\\) is the maximum height, \\(v_0\\) is the initial velocity, \\(v\\) is the final velocity, and \\(a\\) is the acceleration. Plugging in the values we have:\n",
      "\n",
      "   \\[\n",
      "   0 = (20)^2 + 2(-9.81)s\n",
      "   \\]\n",
      "\n",
      "3. **Rearranging the Equation:**\n",
      "   Rearranging this equation to solve for \\(s\\):\n",
      "\n",
      "   \\[\n",
      "   0 = 400 - 19.62s\n",
      "   \\]\n",
      "\n",
      "   \\[\n",
      "   19.62s = 400\n",
      "   \\]\n",
      "\n",
      "   \\[\n",
      "   s = \\frac{400}{19.62}\n",
      "   \\]\n",
      "\n",
      "4. **Calculate the Height:**\n",
      "   Performing the calculation:\n",
      "\n",
      "   \\[\n",
      "   s \\approx 20.39 \\text{ meters}\n",
      "   \\]\n",
      "\n",
      "### Conclusion:\n",
      "The maximum height the ball will reach when thrown upwards with an initial velocity of 20 m/s is approximately **20.39 meters**.\n",
      "\n",
      "Path 2:\n",
      "To solve the problem of how high a ball will go when thrown upwards with an initial velocity of 20 m/s, we can use the principles of kinematics, specifically focusing on the concepts of initial velocity, acceleration due to gravity, and the point at which the ball reaches its maximum height.\n",
      "\n",
      "### Step 1: Understand the situation\n",
      "When the ball is thrown upwards, it will eventually slow down due to the force of gravity acting against its motion. The acceleration due to gravity (g) is approximately -9.81 m/s² (the negative sign indicates that gravity acts in the opposite direction to the motion of the ball).\n",
      "\n",
      "### Step 2: Use the kinematic equation\n",
      "We can use the following kinematic equation to find the maximum height (h) reached by the ball:\n",
      "\n",
      "\\[\n",
      "v^2 = u^2 + 2a s\n",
      "\\]\n",
      "\n",
      "Where:\n",
      "- \\( v \\) = final velocity at the maximum height (0 m/s, since the ball stops rising at that point)\n",
      "- \\( u \\) = initial velocity (20 m/s)\n",
      "- \\( a \\) = acceleration (which is -9.81 m/s²)\n",
      "- \\( s \\) = displacement (maximum height, h)\n",
      "\n",
      "### Step 3: Set up the equation\n",
      "At the maximum height, the final velocity \\( v \\) is 0. Plugging in the values, we get:\n",
      "\n",
      "\\[\n",
      "0 = (20)^2 + 2(-9.81)h\n",
      "\\]\n",
      "\n",
      "### Step 4: Simplify and solve for h\n",
      "This simplifies to:\n",
      "\n",
      "\\[\n",
      "0 = 400 - 19.62h\n",
      "\\]\n",
      "\n",
      "Rearranging gives:\n",
      "\n",
      "\\[\n",
      "19.62h = 400\n",
      "\\]\n",
      "\n",
      "Now, divide both sides by 19.62:\n",
      "\n",
      "\\[\n",
      "h = \\frac{400}{19.62} \\approx 20.39 \\text{ meters}\n",
      "\\]\n",
      "\n",
      "### Conclusion\n",
      "The maximum height the ball will reach is approximately **20.39 meters**. This unique approach clearly outlines the use of kinematic equations to derive the height based on initial conditions and the effects of gravity.\n",
      "\n",
      "Path 3:\n",
      "To solve the problem of how high a ball will go when thrown upwards with an initial velocity of 20 m/s, we can use the principles of kinematics and energy conservation. Here, we'll use energy conservation as our unique approach.\n",
      "\n",
      "### Step 1: Understanding the Energy Conservation Principle\n",
      "\n",
      "When the ball is thrown upwards, it has kinetic energy due to its initial velocity. As it rises, this kinetic energy is converted into gravitational potential energy until it reaches its maximum height, where its velocity becomes zero.\n",
      "\n",
      "### Step 2: Formulating the Energy Equation\n",
      "\n",
      "The kinetic energy (KE) at the moment the ball is thrown can be expressed as:\n",
      "\n",
      "\\[\n",
      "KE = \\frac{1}{2}mv^2\n",
      "\\]\n",
      "\n",
      "where:\n",
      "- \\( m \\) is the mass of the ball,\n",
      "- \\( v \\) is the initial velocity (20 m/s).\n",
      "\n",
      "The gravitational potential energy (PE) at the maximum height can be expressed as:\n",
      "\n",
      "\\[\n",
      "PE = mgh\n",
      "\\]\n",
      "\n",
      "where:\n",
      "- \\( g \\) is the acceleration due to gravity (approximately \\( 9.81 \\, \\text{m/s}^2 \\)),\n",
      "- \\( h \\) is the maximum height reached.\n",
      "\n",
      "### Step 3: Setting Up the Equation\n",
      "\n",
      "At the maximum height, all the kinetic energy will be converted into potential energy:\n",
      "\n",
      "\\[\n",
      "\\frac{1}{2}mv^2 = mgh\n",
      "\\]\n",
      "\n",
      "Notice that the mass \\( m \\) can be canceled from both sides of the equation:\n",
      "\n",
      "\\[\n",
      "\\frac{1}{2}v^2 = gh\n",
      "\\]\n",
      "\n",
      "### Step 4: Solving for Maximum Height\n",
      "\n",
      "Now we can rearrange the equation to solve for \\( h \\):\n",
      "\n",
      "\\[\n",
      "h = \\frac{\\frac{1}{2}v^2}{g}\n",
      "\\]\n",
      "\n",
      "### Step 5: Plugging in the Values\n",
      "\n",
      "Substituting \\( v = 20 \\, \\text{m/s} \\) and \\( g = 9.81 \\, \\text{m/s}^2 \\):\n",
      "\n",
      "\\[\n",
      "h = \\frac{\\frac{1}{2}(20)^2}{9.81}\n",
      "\\]\n",
      "\\[\n",
      "h = \\frac{200}{9.81}\n",
      "\\]\n",
      "\\[\n",
      "h \\approx 20.39 \\, \\text{m}\n",
      "\\]\n",
      "\n",
      "### Conclusion\n",
      "\n",
      "The maximum height the ball will reach is approximately **20.39 meters**. This method effectively utilizes energy conservation principles, providing a unique approach to solving the problem.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "problem = \"A ball is thrown upwards with an initial velocity of 20 m/s. How high will it go?\"\n",
    "paths = generate_multiple_paths(problem)\n",
    "\n",
    "for i, path in enumerate(paths, 1):\n",
    "    print(f\"Path {i}:\\n{path}\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Aggregating Results\n",
    "\n",
    "Now that we have multiple reasoning paths, let's create a function to aggregate the results and determine the most consistent answer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def aggregate_results(paths):\n",
    "    \"\"\"\n",
    "    Aggregate results from multiple reasoning paths.\n",
    "    \n",
    "    Args:\n",
    "    paths (list): List of reasoning paths.\n",
    "    \n",
    "    Returns:\n",
    "    str: The most consistent answer.\n",
    "    \"\"\"\n",
    "    prompt_template = PromptTemplate(\n",
    "        input_variables=[\"paths\"],\n",
    "        template=\"\"\"Analyze the following reasoning paths and determine the most consistent answer. If there are discrepancies, explain why and provide the most likely correct answer.\n",
    "        Reasoning paths:\n",
    "        {paths}\n",
    "        \n",
    "        Most consistent answer:\"\"\"\n",
    "    )\n",
    "\n",
    "    chain = prompt_template | llm\n",
    "    response = chain.invoke({\"paths\": \"\\n\".join(paths)}).content\n",
    "    return response"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's apply this aggregation function to our previous results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Aggregated Result:\n",
      " The most consistent answer across all reasoning paths is that the maximum height the ball will reach when thrown upwards with an initial velocity of 20 m/s is approximately **20.39 meters**.\n",
      "\n",
      "### Analysis of Reasoning Paths:\n",
      "1. **Reasoning Path 1 and Path 2 (Kinematic Equations)**:\n",
      "   - Both paths correctly identify the necessary variables and apply the kinematic equation \\( v^2 = v_0^2 + 2a s \\). They both arrive at the same conclusion through proper rearrangement and calculation.\n",
      "   - The calculations performed in both paths are consistent, leading to the same result of 20.39 meters.\n",
      "\n",
      "2. **Reasoning Path 3 (Energy Conservation)**:\n",
      "   - This path uses a different approach by leveraging the conservation of energy. It starts with kinetic energy and equates it to potential energy at the maximum height.\n",
      "   - The final result of 20.39 meters is consistent with the previous paths, confirming that the calculation is valid regardless of the method used.\n",
      "\n",
      "### Conclusion:\n",
      "Since all reasoning paths lead to the same calculated height of approximately **20.39 meters**, there are no discrepancies among them. The use of different methods (kinematic equations and energy conservation) corroborates the correctness of the result, making it robust and reliable. Thus, the most likely correct answer is indeed **20.39 meters**.\n"
     ]
    }
   ],
   "source": [
    "aggregated_result = aggregate_results(paths)\n",
    "print(\"Aggregated Result:\\n\", aggregated_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Self-Consistency Check\n",
    "\n",
    "To further improve our results, let's implement a self-consistency check that evaluates the reliability of our aggregated answer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def self_consistency_check(problem, aggregated_result):\n",
    "    \"\"\"\n",
    "    Perform a self-consistency check on the aggregated result.\n",
    "    \n",
    "    Args:\n",
    "    problem (str): The original problem statement.\n",
    "    aggregated_result (str): The aggregated result to check.\n",
    "    \n",
    "    Returns:\n",
    "    str: An evaluation of the result's consistency and reliability.\n",
    "    \"\"\"\n",
    "    prompt_template = PromptTemplate(\n",
    "        input_variables=[\"problem\", \"result\"],\n",
    "        template=\"\"\"Evaluate the consistency and reliability of the following result for the given problem.\n",
    "        Problem: {problem}\n",
    "        Result: {result}\n",
    "        \n",
    "        Evaluation (consider factors like logical consistency, adherence to known facts, and potential biases):\"\"\"\n",
    "    )\n",
    "\n",
    "    chain = prompt_template | llm\n",
    "    response = chain.invoke({\"problem\": problem, \"result\": aggregated_result}).content\n",
    "    return response"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, let's apply the self-consistency check to our aggregated result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Self-Consistency Evaluation:\n",
      " ### Evaluation of Consistency and Reliability\n",
      "\n",
      "1. **Logical Consistency**:\n",
      "   - The reasoning paths presented are logically consistent in their approach to solving the problem. Both kinematic equations and energy conservation principles are valid methods for determining the maximum height of a projectile. The fact that all paths arrive at the same numerical result reinforces the logical soundness of the conclusion.\n",
      "\n",
      "2. **Adherence to Known Facts**:\n",
      "   - The use of the kinematic equation \\( v^2 = v_0^2 + 2as \\) and the principle of energy conservation (where kinetic energy at the initial height is converted to potential energy at the maximum height) are both grounded in classical mechanics. The initial velocity of 20 m/s and acceleration due to gravity (approximately -9.81 m/s²) are standard parameters used in projectile motion problems. The calculations are therefore based on known physical laws and principles.\n",
      "\n",
      "3. **Calculation Accuracy**:\n",
      "   - It is important to verify the calculations that lead to the conclusion of 20.39 meters. Using the kinematic equation:\n",
      "     \\[\n",
      "     v^2 = v_0^2 + 2as\n",
      "     \\]\n",
      "     where:\n",
      "     - \\( v \\) (final velocity at the peak) = 0 m/s,\n",
      "     - \\( v_0 \\) (initial velocity) = 20 m/s,\n",
      "     - \\( a \\) (acceleration due to gravity) = -9.81 m/s²,\n",
      "     - \\( s \\) (displacement or maximum height) is what we want to find.\n",
      "\n",
      "     Rearranging gives:\n",
      "     \\[\n",
      "     0 = (20)^2 + 2(-9.81)s\n",
      "     \\]\n",
      "     \\[\n",
      "     0 = 400 - 19.62s\n",
      "     \\]\n",
      "     \\[\n",
      "     19.62s = 400 \\Rightarrow s = \\frac{400}{19.62} \\approx 20.39 \\text{ meters}\n",
      "     \\]\n",
      "\n",
      "   - Similarly, applying energy conservation:\n",
      "     \\[\n",
      "     \\frac{1}{2}mv_0^2 = mgh\n",
      "     \\]\n",
      "     where \\( m \\) cancels out, confirms:\n",
      "     \\[\n",
      "     20^2 = 2gh \\Rightarrow h = \\frac{20^2}{2 \\cdot 9.81} \\approx 20.39 \\text{ meters}\n",
      "     \\]\n",
      "\n",
      "4. **Potential Biases**:\n",
      "   - There appears to be no bias in the reasoning paths, as both methods independently yield the same result. The analysis does not favor one method over the other, ensuring that the conclusion is drawn fairly from multiple approaches.\n",
      "\n",
      "### Conclusion:\n",
      "The result of approximately **20.39 meters** is consistent and reliable based on the analysis provided. The calculations adhere to established physical laws, and the use of different reasoning paths yields the same outcome, reinforcing the accuracy of the conclusion. Therefore, the evaluation confirms that the result can be accepted with confidence.\n"
     ]
    }
   ],
   "source": [
    "consistency_evaluation = self_consistency_check(problem, aggregated_result)\n",
    "print(\"Self-Consistency Evaluation:\\n\", consistency_evaluation)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Applying to Different Problem Types\n",
    "\n",
    "Let's demonstrate how this approach can be applied to different types of problems."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Problem: What is the capital of France?\n",
      "Aggregated Result:\n",
      " The most consistent answer across all three reasoning paths is that the capital of France is **Paris**. \n",
      "\n",
      "### Explanation of Consistency:\n",
      "1. **Identification of the Country**: All reasoning paths correctly identify France as the country in question.\n",
      "2. **Cultural and Historical Significance**: Each path emphasizes the cultural, historical, and political importance of Paris, which is consistent with its designation as the capital.\n",
      "3. **Political Center**: The mention of key political institutions and the central role of Paris in the governance of France is present in all paths.\n",
      "4. **Common Knowledge**: Each reasoning path acknowledges that Paris is widely recognized as the capital, reinforcing the answer through common educational knowledge.\n",
      "\n",
      "### Conclusion:\n",
      "Due to the alignment in identifying Paris as the capital based on cultural, historical, and political significance, as well as its recognition in common knowledge, the most likely correct answer is indeed **Paris**. There are no discrepancies in the reasoning paths that would suggest an alternative answer.\n",
      "\n",
      "Consistency Evaluation:\n",
      " The evaluation of the provided result regarding the capital of France, which is identified as Paris, demonstrates strong consistency and reliability based on several factors. Here’s a detailed assessment:\n",
      "\n",
      "### 1. **Logical Consistency**\n",
      "- Each reasoning path aligns logically with the question posed. The identification of France as the country and Paris as its capital is coherent and follows a rational framework. There are no contradictions in the reasoning processes, which enhances the overall reliability of the conclusion.\n",
      "\n",
      "### 2. **Adherence to Known Facts**\n",
      "- The answer explicitly states that Paris is the capital of France, which is a well-established fact recognized internationally. This aligns with historical, political, and cultural knowledge, making the conclusion factually accurate. The reinforcement of this fact across multiple reasoning paths further solidifies its validity.\n",
      "\n",
      "### 3. **Cultural and Historical Context**\n",
      "- The emphasis on Paris’s cultural, historical, and political significance is pertinent. Not only is Paris the administrative center of France, but it also has a rich heritage that contributes to its status as the capital. This contextualization strengthens the answer and demonstrates a comprehensive understanding of the subject matter.\n",
      "\n",
      "### 4. **Common Knowledge and Consensus**\n",
      "- The recognition of Paris as the capital of France is pervasive in education and general knowledge. The reasoning paths acknowledge this common understanding, which adds another layer of reliability to the conclusion. Consensus on such fundamental knowledge indicates a low probability of error.\n",
      "\n",
      "### 5. **Absence of Bias**\n",
      "- The reasoning paths seem objective and free from biases that might skew the answer. They focus on factual information rather than subjective interpretations, which enhances the credibility of the result.\n",
      "\n",
      "### Conclusion\n",
      "Overall, the evaluation shows that the result of identifying Paris as the capital of France is highly consistent and reliable. The logical structure of the reasoning, adherence to well-known facts, incorporation of relevant cultural and historical context, and absence of bias all contribute to a robust conclusion. Therefore, it can be confidently asserted that the capital of France is indeed **Paris**.\n",
      "\n",
      "--------------------------------------------------\n",
      "\n",
      "Problem: Explain the concept of supply and demand in economics.\n",
      "Aggregated Result:\n",
      " The most consistent answer is that all three reasoning paths illustrate the fundamental concepts of supply and demand in economics through storytelling, but they each present slightly different scenarios that reinforce the same principles.\n",
      "\n",
      "### Analysis of Reasoning Paths\n",
      "\n",
      "1. **Reasoning Path 1** focuses on a bakery scenario, using the relationship between the price of bread and how it affects consumer demand and the baker's supply. It explains the concepts of supply, demand, market equilibrium, and how changes in price impact both sides.\n",
      "\n",
      "2. **Reasoning Path 2** introduces Sally's lemonade stand in Econoville, showcasing a similar dynamic where the price of lemonade affects how much consumers are willing to buy and how much Sally is willing to supply. It illustrates the same concepts of supply and demand with a different product and market condition, including shifts in demand due to external factors like weather.\n",
      "\n",
      "3. **Reasoning Path 3** tells the story of Lucy in a market garden, where the effects of a bountiful harvest and a drought directly influence supply and demand. This narrative also captures the essence of market equilibrium and how external conditions can shift supply and demand.\n",
      "\n",
      "### Consistency and Discrepancies\n",
      "\n",
      "The main consistency across all three paths is the demonstration of the basic economic principles: \n",
      "- **Supply** (the quantity of goods producers are willing to sell at various prices)\n",
      "- **Demand** (the quantity of goods consumers are willing to buy at various prices)\n",
      "- **Market Equilibrium** (where supply equals demand at a certain price)\n",
      "\n",
      "Each path uses a relatable story to express these concepts, making them accessible and understandable. While there are different products (bread, lemonade, vegetables) and scenarios (price changes, weather effects), they all effectively illustrate the same underlying economic principles.\n",
      "\n",
      "### Conclusion\n",
      "\n",
      "The most likely correct answer is that supply and demand are interdependent forces in the marketplace, as illustrated through these narratives. The stories effectively demonstrate how price fluctuations affect both supply and demand, leading to market equilibrium. The consistent theme is the relationship between what producers are willing to sell and what consumers are willing to buy, making the economic principles clear through relatable examples.\n",
      "\n",
      "Consistency Evaluation:\n",
      " The evaluation of the provided result regarding the concept of supply and demand in economics reveals several strengths and some areas for consideration in terms of consistency and reliability.\n",
      "\n",
      "### Strengths:\n",
      "\n",
      "1. **Logical Consistency**: The reasoning paths consistently illustrate the fundamental economic principles of supply and demand. Each scenario is framed within the context of how price influences both consumer demand and producer supply, adhering to the basic tenets of microeconomics.\n",
      "\n",
      "2. **Adherence to Known Facts**: The examples provided (a bakery, a lemonade stand, and a market garden) are all grounded in real-world situations that can be easily understood by a wide audience. They accurately depict how external factors (price changes, weather conditions) can shift supply and demand, which aligns with established economic theories.\n",
      "\n",
      "3. **Clarity of Explanation**: The use of storytelling makes the concepts of supply and demand accessible and relatable. Each path effectively communicates the relationship between price, supply, and demand, which is essential for understanding market dynamics.\n",
      "\n",
      "4. **Illustration of Market Equilibrium**: The consistent mention of market equilibrium across all scenarios reinforces the importance of this concept in economics. It demonstrates how supply and demand interact to determine prices in a market.\n",
      "\n",
      "### Areas for Consideration:\n",
      "\n",
      "1. **Potential Bias in Scenarios**: While all paths are valid, the reliance on common scenarios (like lemonade stands and bakeries) may overlook more complex market dynamics that can exist in real economies. For a comprehensive understanding, it could be beneficial to include examples from various industries or more complex market situations (e.g., monopolies, oligopolies, or global markets).\n",
      "\n",
      "2. **Simplification of Economic Dynamics**: The scenarios presented might simplify some of the complexities of supply and demand. For example, they do not address factors such as consumer preferences, the impact of advertising, or the role of government policies in influencing supply and demand, which are also crucial to a full understanding of these concepts.\n",
      "\n",
      "3. **Assumption of Rational Behavior**: The narratives appear to assume that consumers and producers act rationally, which is a common assumption in economic models. However, actual consumer behavior can be influenced by irrational factors, emotions, or social influences. Highlighting these aspects could provide a more nuanced understanding of the supply and demand framework.\n",
      "\n",
      "### Conclusion:\n",
      "\n",
      "Overall, the result provided is consistent and reliable in explaining the concept of supply and demand in economics. It effectively utilizes relatable scenarios to illustrate fundamental principles while maintaining logical coherence. However, to enhance the evaluation, it would be beneficial to consider more diverse and complex examples, address potential biases, and acknowledge the limitations of the rational actor model. This would lead to a more comprehensive understanding of supply and demand in real-world economics.\n",
      "\n",
      "--------------------------------------------------\n",
      "\n",
      "Problem: If a train travels at 60 km/h, how long will it take to cover 180 km?\n",
      "Aggregated Result:\n",
      " The most consistent answer across the three reasoning paths is that it will take the train **3 hours** to cover 180 km at a speed of 60 km/h.\n",
      "\n",
      "### Explanation of Consistency:\n",
      "1. **Formula Used**: All three reasoning paths rely on the same fundamental relationship between distance, speed, and time, represented by the formula:\n",
      "   \\[\n",
      "   \\text{Time} = \\frac{\\text{Distance}}{\\text{Speed}}\n",
      "   \\]\n",
      "   This consistency in the formula ensures that the basis of the calculations is the same across all paths.\n",
      "\n",
      "2. **Substitution of Values**: Each path correctly identifies the distance as 180 km and the speed as 60 km/h, and correctly substitutes these values into the formula.\n",
      "\n",
      "3. **Calculation**: Each reasoning path performs the division in the same manner, leading to the same result:\n",
      "   \\[\n",
      "   \\text{Time} = \\frac{180 \\text{ km}}{60 \\text{ km/h}} = 3 \\text{ hours}\n",
      "   \\]\n",
      "\n",
      "4. **Conclusion**: Each reasoning path arrives at the same conclusion, affirming that the time required for the train to travel the specified distance at the given speed is indeed 3 hours.\n",
      "\n",
      "### Summary:\n",
      "There are no discrepancies in any of the reasoning paths. They all correctly apply the distance-speed-time relationship and arrive at the same conclusion. Therefore, the most likely correct answer is **3 hours**.\n",
      "\n",
      "Consistency Evaluation:\n",
      " The evaluation of the result regarding how long it will take a train traveling at 60 km/h to cover 180 km can be broken down into several key factors: logical consistency, adherence to known facts, and potential biases.\n",
      "\n",
      "### Logical Consistency:\n",
      "1. **Application of the Formula**: The result is based on the correct application of the distance-speed-time relationship, which is a well-established principle in physics. The formula used, \\( \\text{Time} = \\frac{\\text{Distance}}{\\text{Speed}} \\), is universally accepted and correctly applied here.\n",
      "  \n",
      "2. **Uniform Calculations**: Each reasoning path leading to the final result uses the same mathematical operations to arrive at the conclusion. There is no indication of miscalculation or logical fallacy in any of the paths, reinforcing the reliability of the answer.\n",
      "\n",
      "### Adherence to Known Facts:\n",
      "1. **Known Values**: The values used in the calculations—180 km as the distance and 60 km/h as the speed—are reasonable and typical for train travel, meaning there are no factual errors in the provided data.\n",
      "\n",
      "2. **Correct Interpretation of Units**: The reasoning correctly interprets the units of speed (km/h) and distance (km), leading to a coherent final unit of time (hours).\n",
      "\n",
      "### Potential Biases:\n",
      "1. **Bias in Result Interpretation**: There does not appear to be any bias influencing the interpretation of the result; the answer is purely based on mathematical calculation rather than subjective reasoning.\n",
      "\n",
      "2. **Confirmation Bias**: If there were any external influences or pre-existing beliefs about the train’s speed or distance, those could lead to confirmation bias. However, in this case, the result is strictly based on calculations without any subjective input.\n",
      "\n",
      "### Summary:\n",
      "The evaluation of the reasoning paths shows that they are logically consistent, adhere to known facts, and do not exhibit any identifiable biases. Each path arrives at the same conclusion through sound reasoning, confirming that the answer of **3 hours** is both consistent and reliable. The result is robust against scrutiny, and one can confidently assert that it accurately reflects the time required for the train to cover 180 km at a speed of 60 km/h.\n",
      "\n",
      "--------------------------------------------------\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def solve_problem(problem):\n",
    "    \"\"\"\n",
    "    Solve a problem using multiple reasoning paths, aggregation, and self-consistency check.\n",
    "    \n",
    "    Args:\n",
    "    problem (str): The problem statement.\n",
    "    \n",
    "    Returns:\n",
    "    tuple: (aggregated_result, consistency_evaluation)\n",
    "    \"\"\"\n",
    "    paths = generate_multiple_paths(problem)\n",
    "    aggregated_result = aggregate_results(paths)\n",
    "    consistency_evaluation = self_consistency_check(problem, aggregated_result)\n",
    "    return aggregated_result, consistency_evaluation\n",
    "\n",
    "# Example problems\n",
    "problems = [\n",
    "    \"What is the capital of France?\",\n",
    "    \"Explain the concept of supply and demand in economics.\",\n",
    "    \"If a train travels at 60 km/h, how long will it take to cover 180 km?\"\n",
    "]\n",
    "\n",
    "for problem in problems:\n",
    "    print(f\"Problem: {problem}\")\n",
    "    result, evaluation = solve_problem(problem)\n",
    "    print(\"Aggregated Result:\\n\", result)\n",
    "    print(\"\\nConsistency Evaluation:\\n\", evaluation)\n",
    "    print(\"\\n\" + \"-\"*50 + \"\\n\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
